#include "widget.h"
#include "ui_widget.h"

Widget::Widget(int argc, char *argv[], QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::FramelessWindowHint);

    //tableWidget_order
    ui->tableWidget_order->setColumnWidth(0, 122);
    ui->tableWidget_order->setColumnWidth(1, 180);
    ui->tableWidget_order->setColumnWidth(2, 180);

    //bar_linear
    ui->bar_linear->setSuffix("m/s");
    ui->bar_linear->setMinimum(-1.5);
    ui->bar_linear->setMaximum(1.5);
    //bar_angular
    ui->bar_angular->setSuffix("rad/ks");
    ui->bar_angular->setMinimum(-1);
    ui->bar_angular->setMaximum(1);

    //rpm_charts
    //rpm_charts = new FormCharts(ui->widget_odom_rpm);

    //timeEdit
    QTimer *timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(timerUpdate()));
    timer->start(1000);

    //rostopic
    rosTopic = new ROSTopic(argc, argv);
    rosTopic->start();
    //battery
    connect(rosTopic, &ROSTopic::percentageChanged, this, &Widget::batteryUpdate);
    //odom
    connect(rosTopic, &ROSTopic::linearXChanged, this, &Widget::linearXUpdate);
    connect(rosTopic, &ROSTopic::angularZChanged, this, &Widget::angularZUpdate);
    connect(rosTopic, &ROSTopic::angularXChanged, this, &Widget::angularXUpdate);
    connect(rosTopic, &ROSTopic::leftrmpChanged, this, &Widget::leftrmpUpdate);
    connect(rosTopic, &ROSTopic::rightrmpChanged, this, &Widget::rightrmpUpdate);

    //boocax
    boocaxSocket = new BoocaxSocket();
    boocaxSocket->connectToBoocax();
    connect(boocaxSocket, &BoocaxSocket::basicStatusChanged, this, &Widget::basicStatusUpdate);
    connect(boocaxSocket, &BoocaxSocket::reportPosVelStatusChanged, this, &Widget::reportPosVelStatusUpdate);
    connect(boocaxSocket, &BoocaxSocket::moveStatusChanged, this, &Widget::moveStatusUpdate);
    connect(boocaxSocket, &BoocaxSocket::locStatusChanged, this, &Widget::locStatusUpdate);

    //debug
    ui->toolButton_4->setEnabled(false);
    ui->toolButton_2->setEnabled(false);
}

Widget::~Widget()
{
    delete ui;
    //delete bar_linear;
    //delete bar_angular;
    //delete rpm_charts;
}

void Widget::timerUpdate(void)
{
    ui->dateTimeEdit->setTime(QTime::currentTime());
    QNetworkConfigurationManager mgr;
    if(mgr.isOnline())
    {
        ui->toolButton_network->setEnabled(true);
    }
    else {
        ui->toolButton_network->setEnabled(false);
    }
}

void Widget::batteryUpdate(void)
{
    if(rosTopic->percentage() > 0.95)   ui->toolButton_battery->setIcon(QIcon(":/images/01.png"));
    else if(rosTopic->percentage() > 0.8)   ui->toolButton_battery->setIcon(QIcon(":/images/02.png"));
    else if(rosTopic->percentage() > 0.5)   ui->toolButton_battery->setIcon(QIcon(":/images/03.png"));
    else if(rosTopic->percentage() > 0.25)   ui->toolButton_battery->setIcon(QIcon(":/images/04.png"));
    else if(rosTopic->percentage() > 0.1)   ui->toolButton_battery->setIcon(QIcon(":/images/05.png"));
    else    ui->toolButton_battery->setIcon(QIcon(":/images/06.png"));
    if(rosTopic->charge() > 5)  ui->toolButton_battery->setIcon(QIcon(":/images/08.png"));
}

void Widget::linearXUpdate(void)
{
    ui->bar_linear->setTimerValue(rosTopic->linearX());
    ui->label_vx->setText(QString::number(rosTopic->linearX(),'f',2));
}

void Widget::angularZUpdate(void)
{
    ui->bar_angular->setTimerValue(rosTopic->angularZ());
    ui->label_w->setText(QString::number(rosTopic->angularZ(),'f',2));
}

void Widget::angularXUpdate(void)
{
    ui->label_angular->setText(QString::number(rosTopic->angularX(),'f',2));
}

void Widget::leftrmpUpdate()
{
    ui->widget_odom_rpm->setLeftRPM(rosTopic->leftrmp());
}

void Widget::rightrmpUpdate()
{
    ui->widget_odom_rpm->setRightRPM(rosTopic->rightrmp());
}

void Widget::basicStatusUpdate()
{
    if(boocaxSocket->uwb())
    {
        ui->label_uwb->setStyleSheet("color: rgb(204, 0, 0);");
        ui->label_uwb_point->setStyleSheet("color: rgb(204, 0, 0);");
    }
    else
    {
        ui->label_uwb->setStyleSheet("color: rgb(115, 210, 22);");
        ui->label_uwb_point->setStyleSheet("color: rgb(115, 210, 22);");
    }
    if(boocaxSocket->odom())
    {
        ui->label_odom->setStyleSheet("color: rgb(204, 0, 0);");
        ui->label_odom_point->setStyleSheet("color: rgb(204, 0, 0);");
    }
    else
    {
        ui->label_odom->setStyleSheet("color: rgb(115, 210, 22);");
        ui->label_odom_point->setStyleSheet("color: rgb(115, 210, 22);");
    }
    if(boocaxSocket->base())
    {
        ui->label_base->setStyleSheet("color: rgb(204, 0, 0);");
        ui->label_base_point->setStyleSheet("color: rgb(204, 0, 0);");
    }
    else
    {
        ui->label_base->setStyleSheet("color: rgb(115, 210, 22);");
        ui->label_base_point->setStyleSheet("color: rgb(115, 210, 22);");
    }
    if(boocaxSocket->laser())
    {
        ui->label_lidar->setStyleSheet("color: rgb(204, 0, 0);");
        ui->label_lidar_point->setStyleSheet("color: rgb(204, 0, 0);");
    }
    else
    {
        ui->label_lidar->setStyleSheet("color: rgb(115, 210, 22);");
        ui->label_lidar_point->setStyleSheet("color: rgb(115, 210, 22);");
    }
}


void Widget::reportPosVelStatusUpdate()
{
    ui->label_x->setText(QString::number(boocaxSocket->poseX(),'f',2));
    ui->label_y->setText(QString::number(boocaxSocket->poseY(),'f',2));
    ui->label_yaw->setText(QString::number(boocaxSocket->poseYaw(),'f',2));
    ui->label_v->setText(QString::number(boocaxSocket->velVx(),'f',2));
    ui->label_rad->setText(QString::number(boocaxSocket->velVtheta(),'f',2));
}

void Widget::moveStatusUpdate()
{
    switch (boocaxSocket->moveStatus()) {
    case 0:
        ui->label_move_status->setText("静止待命中");
        ui->label_move_status->setStyleSheet("color: rgb(115, 210, 22);");
        ui->label_move_status_image->setPixmap(QPixmap(":/images/Status_dialog_information.png"));
        break;
    case 1:
        ui->label_move_status->setText("等待新的导航指令");
        ui->label_move_status->setStyleSheet("color: rgb(193, 125, 17);");
        ui->label_move_status_image->setPixmap(QPixmap(":/images/Status_dialog_warning.png"));
        break;
    case 2:
        ui->label_move_status->setText("等待新的导航指令");
        ui->label_move_status->setStyleSheet("color: rgb(115, 210, 22);");
        ui->label_move_status_image->setPixmap(QPixmap(":/images/Status_dialog_information.png"));
        break;
    case 3:
        ui->label_move_status->setText("正在前往目的地");
        ui->label_move_status->setStyleSheet("color: rgb(115, 210, 22);");
        ui->label_move_status_image->setPixmap(QPixmap(":/images/Status_dialog_information.png"));
        break;
    case 4:
        ui->label_move_status->setText("前方障碍物");
        ui->label_move_status->setStyleSheet("color: rgb(204, 0, 0);");
        ui->label_move_status_image->setPixmap(QPixmap(":/images/Status_dialog_error.png"));
        break;
    case 5:
        ui->label_move_status->setText("目的地被遮挡");
        ui->label_move_status->setStyleSheet("color: rgb(204, 0, 0);");
        ui->label_move_status_image->setPixmap(QPixmap(":/images/Status_dialog_error.png"));
        break;
    case 6:
        ui->label_move_status->setText("导航取消");
        ui->label_move_status->setStyleSheet("color: rgb(193, 125, 17);");
        ui->label_move_status_image->setPixmap(QPixmap(":/images/Status_dialog_warning.png"));
        break;
    case 7:
        ui->label_move_status->setText("新目标点");
        ui->label_move_status->setStyleSheet("color: rgb(115, 210, 22);");
        ui->label_move_status_image->setPixmap(QPixmap(":/images/Status_dialog_information.png"));
        break;
    case 8:
        ui->label_move_status->setText("导航路径阻塞");
        ui->label_move_status->setStyleSheet("color: rgb(204, 0, 0);");
        ui->label_move_status_image->setPixmap(QPixmap(":/images/Status_dialog_error.png"));
        break;
    case 10:
        ui->label_move_status->setText("AGV巡线时脱离轨迹");
        ui->label_move_status->setStyleSheet("color: rgb(204, 0, 0);");
        ui->label_move_status_image->setPixmap(QPixmap(":/images/Status_dialog_error.png"));
        break;
    default:
        ui->label_move_status->setText("未知状态，请更新软件");
        ui->label_move_status->setStyleSheet("color: rgb(204, 0, 0);");
        ui->label_move_status_image->setPixmap(QPixmap(":/images/Status_dialog_error.png"));
        break;
    }
}

void Widget::locStatusUpdate()
{
    switch (boocaxSocket->locStatus()) {
    case 0:
        ui->label_loc_status->setText("定位正常");
        ui->label_loc_status->setStyleSheet("color: rgb(115, 210, 22);");
        ui->label_loc_status_image->setPixmap(QPixmap(":/images/Status_dialog_information.png"));
        break;
    case 1:
        ui->label_loc_status->setText("正在尝试定位");
        ui->label_loc_status->setStyleSheet("color: rgb(193, 125, 17);");
        ui->label_loc_status_image->setPixmap(QPixmap(":/images/Status_dialog_warning.png"));
        break;
    case 2:
        ui->label_loc_status->setText("还未构建地图，无法定位");
        ui->label_loc_status->setStyleSheet("color: rgb(204, 0, 0);");
        ui->label_loc_status_image->setPixmap(QPixmap(":/images/Status_dialog_error.png"));
        break;
    case 3:
        ui->label_loc_status->setText("正在构建地图中");
        ui->label_loc_status->setStyleSheet("color: rgb(193, 125, 17);");
        ui->label_loc_status_image->setPixmap(QPixmap(":/images/Status_dialog_warning.png"));
        break;
    case 4:
        ui->label_loc_status->setText("UWB错误");
        ui->label_loc_status->setStyleSheet("color: rgb(204, 0, 0);");
        ui->label_loc_status_image->setPixmap(QPixmap(":/images/Status_dialog_error.png"));
        break;
    case 5:
        ui->label_loc_status->setText("正在进行回环检测");
        ui->label_loc_status->setStyleSheet("color: rgb(193, 125, 17);");
        ui->label_loc_status_image->setPixmap(QPixmap(":/images/Status_dialog_warning.png"));
        break;
    default:
        ui->label_loc_status->setText("未知状态，请更新软件");
        ui->label_loc_status->setStyleSheet("color: rgb(204, 0, 0);");
        ui->label_loc_status_image->setPixmap(QPixmap(":/images/Status_dialog_error.png"));
        break;
    }
}

void Widget::on_toolButton_settings_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_settings);
}

void Widget::on_toolButton_3_clicked()
{
   // QMessageBox::StandardButton reply = QMessageBox::warning(NULL,"退出软件","系统将关闭本软件，RIVAMED机器人系统将会失效！", QMessageBox::Yes|QMessageBox::No,QMessageBox::No);
    //if(reply == QMessageBox::Yes)
   // {
        rosTopic->quit();
        this->close();
   // }
}

void Widget::on_toolButton_clicked()
{
    QMessageBox::StandardButton reply = QMessageBox::warning(NULL,"重启系统","系统将被重启！", QMessageBox::Yes|QMessageBox::No,QMessageBox::No);
    if(reply == QMessageBox::Yes)
    {
        std::system("reboot");
    }
}

void Widget::on_toolButton_5_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_main);
}
